package com.itfreer.bpm.workflow.service;

import java.io.IOException;
import java.io.OutputStream;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.flow.api.IBpmWorkFlowImageService;

@Component(value="processImage")
@Path("/processImage")
public class RestBpmWorkFlowImageService {
	
	@Autowired
	IBpmWorkFlowImageService imaseServie;
	/**
	 * 刷新流程的图片信息
	 * @param bucketName
	 * @param objectKey
	 * @return
	 * @throws Exception
	 */
	@GET
	@Path("/loadImage")
	@Produces({MediaType.APPLICATION_OCTET_STREAM})
	public Response loadWorkFlowImage(@QueryParam("bpmkey") String bpmkey, 
			@QueryParam("sexeid") String sexeid) throws Exception {
		byte[] result = imaseServie.loadWorkFlowImage(sexeid, bpmkey);
		StreamingOutput stream = new StreamingOutput() {
			@Override
			public void write(OutputStream output) throws IOException, WebApplicationException {
				try {
					output.write(result);
	            } catch (Exception e) {
	                throw new WebApplicationException(e);
	            }
			}
	    };
	    return Response.ok(stream)
	    		.header("content-disposition","attachment;")
	    		.header("Content-Type","image/png;")
	    		.header("Cache-Control", "no-cache").build();
	}
}
